let app = getApp();
let API_URL = app.globalData.API_URL;
let iconUrl = 'https://atush.vip/static/icons/';

Page({

    /**
     * 页面的初始数据
     */
    data: {
        lang: null,
        showUnfilledDialog: false,
        showSuccessPromptDialog: false,
        userId: '',
        iconUrl: iconUrl,

        uploadImages: [],
        files: [],
        // 填空
        infoObj :{}
    },

    // my function

    sendBTN() {
        let title = this.data.infoObj.title
        let content = this.data.infoObj.complaintContent
        let userId = this.data.userId
        let data = {
            user_id: userId,
            title,
            content
        }
        if (
            title == undefined ||
            title == '' ||
            content == undefined ||
            content == ''
        ) {
            return this.setData({ showUnfilledDialog: true })
        }
        let that = this
        wx.request({
            url: `${API_URL}/complaint`, 
            data: data,
            method: 'post',
            success: function (res) {
                that.uploadTempImages(res.data.insertId, 19);
                that.setData({ showSuccessPromptDialog: true, infoObj: {} ,uploadImages: [] });
            }
        })
    },

    // 上传照片
    uploadTempImages(targetId, targetType) {
        for (const item of this.data.uploadImages) {
            wx.uploadFile({
                url: `${API_URL}/houseFiles`,
                filePath: item.fullUrl,
                name: 'file',
                formData: {
                    'target_id': targetId,
                    'target_type': targetType
                },
                success(res) {
                    const data = res.data
                }
            })
        }
    },

    // 添加照片
    uploadImage() {
        let that = this;
        let imgArr = this.data.uploadImages;
        wx.chooseImage({
            count: 5,
            success(res) {
                const tempFilePaths = res.tempFilePaths
                for (const item of tempFilePaths) {
                    if ( imgArr.length == 5 ) {
                        console.log('已经 5 个照片了');
                        break
                    }
                    let data = {}
                    data.fullUrl = item
                    imgArr.push(data)
                }
                that.setData({
                    uploadImages: imgArr
                })
            }
        })
    },

    title(e){
        this.data.infoObj.title = e.detail.value
        this.setData({infoObj : this.data.infoObj})
    },

    complaintContent(e){
        this.data.infoObj.complaintContent = e.detail.value
        this.setData({infoObj : this.data.infoObj})
    },

    // 完毕对话框
    dialogOnComplete() {
        this.setData({ showUnfilledDialog: false })
    },

    // 成功提示框完毕
    completeDialog() {
        this.setData({ showSuccessPromptDialog: false })
    },

    // 移除图片
    destroyPhoto(e) {
        let index = e.currentTarget.dataset.index
        let arr = this.data.uploadImages
        arr.splice(index, 1)
        this.setData({ uploadImages: arr })
    },

    // app function

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({ lang: app.globalData.lang, navTitle: "complaint", navigation: true, userId: wx.getStorageSync('uid') })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})